<?php

class adminController extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->view->render('admin/index');
    }

    function updateMenu($params = array()) {
        $id = _get($params, 'id');
        $db = Model_Menu::getInstance();

        if (_get($params, 'submit') != null) {
            $name = _get($params, 'name');
            $price = _get($params, 'price');
            $code = _get($params, 'code');
            $values = array('name' => $name, 'price' => $price,'code'=>$code);
            if (isset($_FILES) && isset($_FILES['files']) && $_FILES['files']['name'] != null) {
                $upload = new UploadHandler();
                $filename = $upload->post();
                $values['imgpath'] = $filename;
            }
            $db->update($values, array('id' => $id));
        }
        $result = $db->select('all', array('id' => $id));
        $this->view->render('admin/updateMenu', array('menu' => $result));
    }

    function addTable($params = array()) {
        $name = _get($params, 'name');
        $db = Model_Table::getInstance();
        $values = array('id' => null, 'label' => $name);
        $db->insert($values);
        $this->view->render('admin/index');
    }

    function addMenu($params = array()) {
        $name = _get($params, 'name');
        $price = _get($params, 'price');
        $type = _get($params, 'type');
        $db = Model_Menu::getInstance();
        $values = array('id' => null, 'name' => $name, 'price' => $price, "type" => $type);
        if (isset($_FILES)) {
            $upload = new UploadHandler();
            $filename = $upload->post();
            $values['imgpath'] = $filename;
        }

        $db->insert($values);
        $this->view->render('admin/index');
    }

    public function upload() {
        $this->view->render('admin/upload');
    }

    public function result() {
        $this->view->render('admin/result');
    }

    public function doUpload() {
        error_reporting(E_ALL | E_STRICT);
        $upload_handler = new UploadHandler();
        $upload_handler->post();
        $this->view->render('admin/result');
    }

    public function listLocal() {
        $db_local = Model_Local::getInstance();
        $data['local'] = $db_local->selectAll();

        $this->view->render('admin/listLocal', $data);
    }

    public function addLocal($params) {
        $db_local = Model_Local::getInstance();
        if (_get($params, 'submit')) {
            $default = _get($params, 'default');
            $en = _get($params, 'en');
            $kh = _get($params, 'kh');
            $db_local->insert(array('def' => $default, 'en' => $en, 'kh' => $kh));
        }
        $this->view->render('admin/addLocal');
    }

}

?>
